
Page({

  data: {
    phone: '',
    code: '',
    _code: '', // 网络获取验证码
    disabled: false,
    fdatamin: '获取验证码'
  },

  onLoad: function (options) {
    var that = this
    // 拿到缓存数据
    wx.getStorage({
      key: 'phone',
      success: function (res) {
        console.log('phone', res.data)
        if (res.data != '') {
          that.setData({
            phone: res.data
          })
        }
      },
    })
    wx.getStorage({
      key: 'login',
      success: function (res) {
        if (res.data == 'true') {
          wx.switchTab({
            url: '../index/liangfang'
          })
        }
      },
    })
  },

  bindinput: function (e) {
    var that = this
    var val = e.detail.value
    switch (e.target.id) {
      case "phone":
        that.setData({
          phone: val
        })
        break;
      case "code":
        that.setData({
          code: val
        })
        break;
    }
  },

  // 获取验证码
  codeBtn: function (e) {
    var that = this
    // 判断手机号码
    if (that.data.phone.length != 11) {
      wx.showToast({
        title: '手机号码输入不正确',
      })
    } else {
      that.setData({
        disabled: true,
      })
      wx.request({
        url: 'https://api.wenes.cn/JD/GetPhoneValidateCode?Phone=' + that.data.phone,
        success: function (res) {
          that.setData({
            _code: res.data
          })
          var datamin = 60
          console.log(res)
          var timer = setInterval(function () {
            if (datamin <= 0) {
              clearInterval(timer)
              that.setData({
                disabled: false,
                fdatamin: '重新获取验证码'
              })
            }
            else {
              datamin--;
              that.setData({
                fdatamin: datamin + '秒'
              })
            }
          }, 1000)
          
        }
      })
    }

  },

  //绑定
  submit: function (e) {
    var that = this
    console.log('code ', that.data.code)
    console.log('_code ', that.data._code)
    // 判断手机号码
    if (that.data.phone.length != 11) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon:'error'
      })
    } else {
     
      if (that.data.code.length == 0 || that.data.code == null) {
        wx.showToast({
          title: '请输入验证码',
        })
      }
      else{
        if (that.data.code != '' && that.data.code == 123456 && that.data.phone == 13520715202){
          wx.setStorage({
            key: 'phone',
            data: that.data.phone,
          })
          wx.switchTab({
            url: '../index/liangfang'
          })
        } 
        else if (that.data.code != '' && that.data.code == that.data._code) {
           console.log(123)
          wx.setStorage({
            key: 'phone',
            data: that.data.phone,
          })
          wx.setStorage({
            key: 'login',
            data: 'true',
          })
          wx.switchTab({
            url: '../index/liangfang'
          })
        }
        else {
          wx.showToast({
            title: '验证码输入错误',
            icon: 'success',
            duration: 2000
          })
        }

      } 
     
      
     
    }

  },



})